package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.ClassAdmins;
import com.xjc.business.entity.domain.CollageClazzAdminRelations;
import com.xjc.business.entity.domain.DoorAdmins;
import com.xjc.business.entity.domain.SchoolDoorAdminRelations;
import com.xjc.business.entity.po.dooradmin.DoorAdminLoginPo;
import com.xjc.business.entity.po.schadmin.PersonPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.classadmin.ClassAdminLoginVo;
import com.xjc.business.entity.vo.dooradmin.DoorAdminLoginVo;
import com.xjc.business.entity.vo.schadmin.PersonVo;
import com.xjc.business.service.DoorAdminsService;
import com.xjc.business.mapper.DoorAdminsMapper;
import com.xjc.business.service.SchoolDoorAdminRelationsService;
import com.xjc.business.service.SchoolSetsService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import com.xjc.system.utils.BcryptUtil;
import com.xjc.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【door_admins(门卫)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:31
*/
@Service
public class DoorAdminsServiceImpl extends ServiceImpl<DoorAdminsMapper, DoorAdmins>
    implements DoorAdminsService {

    @Autowired
    @Lazy
    SchoolDoorAdminRelationsService schoolDoorAdminRelationsService;

    @Autowired
    @Lazy
    SchoolSetsService schoolSetsService;

    /**
     * 分页查询人员信息
     *
     * @param pageSize      每页显示条数
     * @param currentPage   当前页码
     * @param adminUserName 用户名
     * @param adminNickName 昵称
     * @param schoolId
     * @return 信息
     */
    @Override
    public Result<PageVo<PersonVo>> getDoorAdminPage(Integer pageSize, Integer currentPage, String adminUserName, String adminNickName, Integer schoolId) {

        LambdaQueryWrapper<SchoolDoorAdminRelations> schoolDoorAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolDoorAdminRelationsLambdaQueryWrapper.eq(SchoolDoorAdminRelations::getSchId, schoolId);
        List<SchoolDoorAdminRelations> schoolDoorAdminRelations = schoolDoorAdminRelationsService.list(schoolDoorAdminRelationsLambdaQueryWrapper);
        List<Integer> collectIdList = schoolDoorAdminRelations.stream().map(SchoolDoorAdminRelations::getDoorAdminId).collect(Collectors.toList());

        PageVo<PersonVo> pageVo = new PageVo<>();

        if(collectIdList.isEmpty()){
            return Result.okWithData(pageVo);
        }

        LambdaQueryWrapper<DoorAdmins> doorAdminsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        doorAdminsLambdaQueryWrapper.like(!adminUserName.isEmpty(), DoorAdmins::getUsername, adminUserName);
        doorAdminsLambdaQueryWrapper.like(!adminNickName.isEmpty(), DoorAdmins::getNickname, adminNickName);
        Page<DoorAdmins> doorAdminsPage = new Page<>(currentPage, pageSize);
        page(doorAdminsPage, doorAdminsLambdaQueryWrapper);
        List<DoorAdmins> doorAdminsPageRecords = doorAdminsPage.getRecords();
        List<PersonVo> personVoList = doorAdminsPageRecords.stream().map(doorAdmins -> {
            PersonVo personVo = new PersonVo();
            personVo.setId(doorAdmins.getId());
            personVo.setUsername(doorAdmins.getUsername());
            personVo.setNickname(doorAdmins.getNickname());
            personVo.setPic(doorAdmins.getPic());
            personVo.setPhone(doorAdmins.getPhone());
            personVo.setEmail(doorAdmins.getEmail());
            personVo.setStatus(MyConstants.USER_STATUS.get(doorAdmins.getStatus()));
            personVo.setCreateTime(doorAdmins.getCreateTime());
            personVo.setType(2);
            return personVo;
        }).collect(Collectors.toList());
        pageVo.setData(personVoList);
        pageVo.setPageCount(doorAdminsPage.getPages());
        pageVo.setSize(pageSize);
        pageVo.setTotal(doorAdminsPage.getTotal());
        pageVo.setCurrent(currentPage);
        return Result.okWithData(pageVo);
    }

    /**
     * 添加管理员
     *
     * @param personPo 管理员信息
     * @return 反馈
     */
    @Override
    @Transactional
    public Result<String> addDoorAdmin(PersonPo personPo) {
        DoorAdmins doorAdmins = new DoorAdmins();
        doorAdmins.setUsername(personPo.getUsername());
        doorAdmins.setPassword(BcryptUtil.hashPassword(personPo.getPassword()));
        doorAdmins.setNickname(personPo.getNickname());
        doorAdmins.setPhone(personPo.getPhone());
        doorAdmins.setEmail(personPo.getEmail());
        doorAdmins.setCreateTime(new Date());
        doorAdmins.setUpdateTime(new Date());
        doorAdmins.setStatus(1);
        doorAdmins.setPic(personPo.getPic());
        save(doorAdmins);

        SchoolDoorAdminRelations schoolDoorAdminRelations = new SchoolDoorAdminRelations();
        schoolDoorAdminRelations.setSchId(personPo.getSchoolId());
        schoolDoorAdminRelations.setDoorAdminId(doorAdmins.getId());
        schoolDoorAdminRelationsService.save(schoolDoorAdminRelations);

        return Result.okWithData("增加成功");
    }

    /**
     * 登录
     * @param doorAdminLoginPo 登录参数
     * @return 登录成功返回token，失败返回错误信息
     */
    @Override
    public Result<DoorAdminLoginVo> doLogin(DoorAdminLoginPo doorAdminLoginPo) {
        String password = doorAdminLoginPo.getPassword();
        String username = doorAdminLoginPo.getUsername();

        LambdaQueryWrapper<DoorAdmins> doorAdminsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        doorAdminsLambdaQueryWrapper.eq(DoorAdmins::getUsername, username);
        DoorAdmins doorAdminsFromDB = getOne(doorAdminsLambdaQueryWrapper);

        if (doorAdminsFromDB == null) {
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.USERNAME_ERROR);
        }

        if (!(BcryptUtil.checkPassword(password,doorAdminsFromDB.getPassword()))){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PASSWORD_ERROR);
        }


        if (!schoolSetsService.sureHasSet(MyConstants.DOOR_ROLE,doorAdminsFromDB.getId())) {
            throw new MyException(ResultCode.SUCCESS.getCode(),ResultCode.NOTOPEANSET.getMessage());
        }

        DoorAdminLoginVo doorAdminLoginVo = new DoorAdminLoginVo();
        doorAdminLoginVo.setId(doorAdminsFromDB.getId());
        doorAdminLoginVo.setUsername(doorAdminsFromDB.getUsername());
        doorAdminLoginVo.setNickname(doorAdminsFromDB.getNickname());
        doorAdminLoginVo.setPic(doorAdminsFromDB.getPic());
        doorAdminLoginVo.setToken(TokenUtils.generateToken(doorAdminsFromDB.getId()));

        LambdaQueryWrapper<SchoolDoorAdminRelations> doorAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        doorAdminRelationsLambdaQueryWrapper.eq(SchoolDoorAdminRelations::getDoorAdminId,doorAdminsFromDB.getId());

        SchoolDoorAdminRelations schoolDoorAdminRelations = schoolDoorAdminRelationsService.getOne(doorAdminRelationsLambdaQueryWrapper);

        if(schoolDoorAdminRelations !=null){
            doorAdminLoginVo.setSchoolId(schoolDoorAdminRelations.getSchId());
        }

        return Result.okWithData(doorAdminLoginVo);
    }
}




